<template>
  <div class="saveUpdate config">
    <el-dialog :title="DetailInfo.id?'编辑配置':'新增配置'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="1000px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="120px" size="mini">
        <dialog-wrapper style="width:900px; max-width: 900px">

        <el-form-item v-if="DetailInfo.id" label="日期:"  prop="day" :rules="[{ required: true, message: '选择日期不能为空'}]">
            <el-date-picker  :disabled="DetailInfo.id&&true"   v-model="DetailInfo.day" value-format="yyyy-MM-dd"
                             type="date" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
          <el-form-item class="date" v-if="!DetailInfo.id" label="选择日期:" prop="start_day" :rules="[{ required: true, message: '选择日期不能为空'}]">
            <el-date-picker :disabled="DetailInfo.id"  v-model="DetailInfo.start_day" value-format="yyyy-MM-dd"
                            type="date" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
          <el-form-item class="date" v-if="!DetailInfo.id"     prop="end_day" :rules="[{ required: true, message: '选择日期不能为空'}]">
            <span style="float: left;display: inline-block;">-</span>
             <el-date-picker  :disabled="DetailInfo.id"   v-model="DetailInfo.end_day" value-format="yyyy-MM-dd"
                              type="date" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
          <el-form-item label="选择主管:" prop="manager_list"   :rules="[{ required: true, message: '选择主管不能为空'}]">
            <el-select  :disabled="DetailInfo.id&&true"  v-model="DetailInfo.manager_list" :multiple="!DetailInfo.id" placeholder="请选择">
              <el-option v-for="item in managerlist" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="当日激活:" class="_item"    prop="register_count"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.register_count.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="register_count"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.register_count.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>

          <el-form-item label="当日注册:" prop="bind_count"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.bind_count.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="bind_count"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.bind_count.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="首充人数:"   prop="first_recharge_user"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.first_recharge_user.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="first_recharge_user"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.first_recharge_user.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>

          <el-form-item label="首充金额:"  prop="first_recharge_count"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.first_recharge_count.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="first_recharge_count"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.first_recharge_count.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="新充人数:"  prop="recharge_new_user"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.recharge_new_user.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="recharge_new_user"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.recharge_new_user.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="新充金额:"  prop="recharge_new_amount"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.recharge_new_amount.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="recharge_new_amount"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.recharge_new_amount.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="充值人数:"   prop="recharge_user"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.recharge_user.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="recharge_user"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.recharge_user.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="充值金额:"   prop="recharge_amount"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.recharge_amount.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="recharge_amount"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.recharge_amount.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="代理充值:"   prop="agency_recharge"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.agency_recharge.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="agency_recharge"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.agency_recharge.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="在线充值:"   prop="online_recharge"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.online_recharge.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="online_recharge"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.online_recharge.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="当日提现:"  prop="withdraw_amount"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.withdraw_amount.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="withdraw_amount"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.withdraw_amount.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


          <el-form-item label="实际税收:"   prop="real_channel_tax_amount"  :rules="adjustmentrules">
            <el-select v-model="DetailInfo.adjustment_dct.real_channel_tax_amount.adjustment_by"  placeholder="请选择">
              <el-option v-for="(value,key) of  adjustmentList" :key="key" :label="value" :value="key" />
            </el-select>
          </el-form-item>

          <el-form-item class="set"  prop="real_channel_tax_amount"  :rules="ratiorules">
            <el-input v-model="DetailInfo.adjustment_dct.real_channel_tax_amount.ratio"  placeholder="设置系数"></el-input>
          </el-form-item>


        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="DetailInfo.id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import { updateConfigInfo, saveAddConfigInfo } from '@/api'
  import {queryFilter} from '@/utils/patch'
  export default {
    components: { DialogWrapper },
    data() {
      let validAdjustment = (rule, value, callback) => {
        if (!this.DetailInfo.adjustment_dct[rule.field].adjustment_by) {
          callback(new Error(`${this.adjustmentList[rule.field]}不能为空`))
        } else {
          callback()
        }
      }
      let validRatio = (rule, value, callback) => {
        let ratio=this.DetailInfo.adjustment_dct[rule.field]
        if (!ratio&&ratio!==0) {
          callback(new Error(`系数不能为空`))
        } else {
          callback()
        }
      }
      return {
        showDialog: this.showUpdate,
        loading: false,
        adjustment_dct:{
          bind_count:{
            adjustment_by:'first_recharge_user'
          },
          register_count:{
            adjustment_by:'first_recharge_user'
          },
          first_recharge_count:{
            adjustment_by:'first_recharge_count'
          },
          first_recharge_user:{
            adjustment_by:'first_recharge_user'
          },
          recharge_new_user:{
            adjustment_by:'first_recharge_user'
          },
          recharge_user:{
            adjustment_by:'first_recharge_user'
          },

          recharge_amount:{
            adjustment_by:'first_recharge_count'
          },
          recharge_new_amount:{
            adjustment_by:'first_recharge_count'
          },
          agency_recharge:{
            adjustment_by:'first_recharge_count'
          },
          online_recharge:{
            adjustment_by:'first_recharge_count'
          },
          withdraw_amount:{
            adjustment_by:'first_recharge_count'
          },
          real_channel_tax_amount:{
            adjustment_by:'first_recharge_count'
          }
        },
        DetailInfo: {
        },
        showParent: false,
        Parents: [],
        adjustmentrules:[ { validator: validAdjustment, trigger: 'blur' }],
        ratiorules:[ { validator: validRatio, trigger: 'blur' }],

      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      Detail: {
        type: Object
      },
      managerlist: {
        type: Array
      },
      rolelist: {
        type: Object
      },
      adjustmentList:{
        type:Object
      }
    },
    created() {
      if(this.Detail.id){
        let{day,id,manager,adjustment_dct}=this.Detail
        Object.assign(this.DetailInfo,{day,manager_list:manager,id,adjustment_dct})
      }else{
        Object.assign(this.DetailInfo,{adjustment_dct:this.adjustment_dct})
      }
    },
    methods: {
      saveDetail(){
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            Reflect.ownKeys(this.DetailInfo.adjustment_dct).map(item=>{
              this.DetailInfo.adjustment_dct[item].ratio= Number(this.DetailInfo.adjustment_dct[item].ratio)
            })
            let adjustment_dct=`${JSON.stringify(this.DetailInfo.adjustment_dct)}`
           let data = Object.assign({}, this.DetailInfo, {adjustment_dct} )
            saveAddConfigInfo(data).then(res => {
              if(res.status === 0){
                this.$message({ message: '新增成功。',type: 'success' })
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      updateDetail(){
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            Reflect.ownKeys(this.DetailInfo.adjustment_dct).map(item=>{
                this.DetailInfo.adjustment_dct[item].ratio= Number(this.DetailInfo.adjustment_dct[item].ratio)
            })
            let adjustment_dct=`${JSON.stringify(this.DetailInfo.adjustment_dct)}`

            updateConfigInfo({adjustment_id:this.DetailInfo.id,adjustment_dct}).then(res => {
              if(res.status === 0){
                this.$message({ message: '编辑成功。',type: 'success' })
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss">
.saveUpdate.config{

.el-form-item{
  width: 450px;
  float: left;
}
.el-form-item:nth-child(1).date{
  width: 270px;
}
.el-form-item:nth-child(2).date{
  width: 180px;
    .el-date-editor--date{
        width: 95%;
    }
    .el-form-item__content{
        margin-left: 0px !important;
    }
}

.el-form-item:nth-child(3)~div.el-form-item:not(.set), ._item{
  width: 325px;
}
.set{
  width:125px;
}
.set .el-form-item__content{
  margin-left: 5px !important;
}
}
</style>
